package com.df.bpm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.df.common.service.impl.BaseServiceImpl;
import com.df.bpm.entity.InformatizationTarget;
import com.df.bpm.mapper.InformatizationTargetMapper;
import com.df.bpm.service.InformatizationTargetService;

import com.df.common.utils.CommonUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 需求预期目标服务
 */
@Service
@RequiredArgsConstructor
public class InformatizationTargetServiceImpl extends BaseServiceImpl<InformatizationTargetMapper,InformatizationTarget> implements InformatizationTargetService {
	public final InformatizationTargetMapper informatizationTargetMapper;

    public void saveTargets4RequirementId(Integer requirementId, List<InformatizationTarget> targets) {
        StringBuilder targetsId= null;
        for(InformatizationTarget target:targets){
            Integer index=target.getInformatizationTargetIndex();
            if(CommonUtils.varIsNotBlank(index)){
                if(targetsId==null){
                    targetsId = new StringBuilder("" + index);
                }else{
                    targetsId.append(",").append(index);
                }
            }
        }
        if(CommonUtils.varIsNotBlank(targetsId)){
            informatizationTargetMapper.delete4Save(requirementId,targetsId.toString());
        }else{
            informatizationTargetMapper.delete(new QueryWrapper<InformatizationTarget>().eq("informatizationRequirementIndex",requirementId));
        }
        for(InformatizationTarget target:targets){
            if(CommonUtils.varIsNotBlank(target.getInformatizationTargetIndex())){
                if(CommonUtils.varIsNotBlank(target.getTarget()) &&
                        !"-".equals(target.getTarget())) {
                    informatizationTargetMapper.updateById(target);
                }else{
                    informatizationTargetMapper.deleteById(target);
                }
            }else{
                if(CommonUtils.varIsNotBlank(target.getTarget())){
                    target.setInformatizationRequirementIndex(requirementId);
                    informatizationTargetMapper.insert(target);
                }
            }
        }
    }
}